<?php
/**
**百度直达号
**核销通知： check，
**订单完结通知： complete，
**退票审核通知： refund
**/

include('/var/www/html/new/d/module/api/function.class.php');
include('/var/www/html/new/d/module/api/bd_zhidahao/baidu_zdh.class.php');

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$BD_zdh = new BD_zdh();
$PFT_infunction = new AllFunction();

$cmd = $_REQUEST['cmd'];
if($cmd == 'refund'){ //退款结果通知
	$auditId = $_REQUEST['auditId'];
	$UUsellid = $_REQUEST['UUsellid'];
	$status = $_REQUEST['status'] == 100 ? 'success' : 'fail' ;
	
	$sel = "select supplierIdentity,signkey from uu_qunar_use where DockingMode=2 and fid=$UUsellid limit 1";
	$GLOBALS['le']->query($sel);
	$GLOBALS['le']->fetch_assoc();	
	$prodiver_no = $GLOBALS['le']->f('supplierIdentity');
	$signkeys = explode("|",$GLOBALS['le']->f('signkey'));
	$sp_no = $signkeys[0];
	$secret_key = $signkeys[1];
	
	$msg = '{"auditId":"'.$auditId.'","status":"'.$status.'"}';
	$result = bd_zhida_post($msg,'refund',$BD_zdh,$prodiver_no,$secret_key);
	$BD_zdh->logfile(json_encode($_REQUEST).'|'.$msg.'|'.json_encode($result),'refundNote');
	
}elseif($cmd == 'check'){ //订单核销通知
	
	$orderId = $_REQUEST['orderId'];
	$subOrder = $_REQUEST['subOrder'];
	$Order16U = $_REQUEST['Order16U']; //票付通订单号
	$Tnumber = $_REQUEST['Tnumber'];  //验证票数
	$time = $_REQUEST['time'];  //验证时间
	
	$pft_order_xml  = $PFT_infunction->Select_PFTorder($Order16U,22196);
	$UUsellid= (int)$pft_order_xml->Rec->UUsellid;
	$sel = "select supplierIdentity,signkey from uu_qunar_use where DockingMode=2 and fid=$UUsellid limit 1";
	$GLOBALS['le']->query($sel);
	$GLOBALS['le']->fetch_assoc();	
	$prodiver_no = $GLOBALS['le']->f('supplierIdentity');
	$signkeys = explode("|",$GLOBALS['le']->f('signkey'));
	$sp_no = $signkeys[0];
	$secret_key = $signkeys[1];
	$arr = array(
		'orderId'		=> $orderId,
		'subOrderId'	=> $subOrder,
		'checkTime'		=> $time,
		'checkNum'		=> $Tnumber,
	);	
	$msg = json_encode($arr);
	$result = bd_zhida_post($msg,'check',$BD_zdh,$prodiver_no,$secret_key);
	$BD_zdh->logfile(json_encode($_REQUEST).'|'.$msg.'|'.json_encode($result),'checkNote');
	
	// if($result['result'] == 1){ //要发订完完结通知
		// $arr = array(
			// 'orderId'		=> $orderId,
			// 'successTime'	=> $time,
		// );
		// $msg = json_encode($arr);
		// $result = bd_zhida_post($msg,'complete',$BD_zdh,$prodiver_no,$secret_key);
		// $BD_zdh->logfile($msg.'|'.json_encode($result),'completeNote');
	// }
}


function bd_zhida_post($msg,$cmd,$BD_zdh,$prodiver_no,$secret_key){  

	$timestamp = time();
	$sign = md5('cmd='.$cmd.'msg='.$msg.'provider_no='.$prodiver_no.'timestamp='.$timestamp.$secret_key);
	$response_all = array(
		'cmd' 	=> $cmd ,
		'msg'	=> $msg ,
		'timestamp'		=> $timestamp ,
		'provider_no' 	=> $prodiver_no ,
		'sign'	=> $sign 
	);	
	// $RqUrl ='http://180.149.144.147:8173/lightapp/pay/ticket/provider/notice';
	$RqUrl ='http://m.baidu.com/lightapp/pay/ticket/provider/notice';
	$str= http_build_query($response_all);
	// print_r($response_all);exit;
	$ch = curl_init();//打开
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_URL, $RqUrl);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
	$response  = curl_exec($ch);
	curl_close($ch);//关闭 
	$result = json_decode($response,true);
	return $result;
}	
?>>